#include <iostream>

using namespace std;

#define InitSize 20
#define IncSize 10

template <typename T>
class SeqStack
{
public:
    SeqStack(int length = InitSize); // 构造函数
    ~SeqStack();                     // 析构函数

public:
    bool push(const T &e); // 入栈
    bool pop(T &e);        // 出栈
    bool peek(T &e);       // 取栈顶元素
    void print();          // 打印栈
    int len();             // 返回栈长度
    bool isEmpty();        // 判断栈是否为空
    bool isFull();          // 判断栈是否已满
private:
    void IncreaseSize(); // 栈满时，扩容

private:
    T *m_data;     // 栈元素
    int m_maxsize; // 栈最大长度
    int m_top;     // 栈顶指针
};

template <typename T>
SeqStack<T>::SeqStack(int length)
{
    m_data = new T[length];
    m_maxsize = length;
    m_top = -1;
}

template <typename T>
SeqStack<T>::~SeqStack()
{
    delete[] m_data;
}

template <typename T>
bool SeqStack<T>::push(const T &e)
{
    if (isFull() == true)
    {
        IncreaseSize();
    }
    m_top++;
    m_data[m_top] = e;
    return true;
}

template <typename T>
bool SeqStack<T>::pop(T &e)
{
    if (isEmpty() == true)
    {
        cout << "栈为空" << endl;
        return false;
    }
    e = m_data[m_top];
    m_top--;
    return true;
}

template <typename T>
bool SeqStack<T>::peek(T &e)
{
    if (isEmpty() == true)
    {
        cout << "栈为空" << endl;
        return false;
    }
    e = m_data[m_top];
    return true;
}

template <class T>
void SeqStack<T>::print()
{
    for (int i = m_top; i > 0; i--)
    {
        cout << m_data[i] << " ";
    }
    cout << endl;
}

template <class T>
int SeqStack<T>::len()
{
    return m_top + 1;
}

template <class T>
bool SeqStack<T>::isEmpty()
{
    if (m_top == -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class T>
int SeqStack<T>::isFull()
{
    if (m_top == m_maxsize - 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}


template <class T>
void SeqStack<T>::IncreaseSize()
{
    T *p = m_data;
    m_data = new T[m_maxsize + IncSize];
    for (int i = 0; i <= m_top; i++)
    {
        m_data[i] = p[i];
    }
    m_maxsize += IncSize;
    delete[] p;
}

int main()
{

    SeqStack<int> stack;

    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);

    stack.print();

    int tmp = 0;
    stack.pop(tmp);
    cout << tmp << endl;
    stack.pop(tmp);
    cout << tmp << endl;

    cout << "------------------" << endl;

    stack.print();



}